<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="80px">
          <div class="search-row" >
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable
                        @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
          <div class="search-row">
            <el-form-item
              label="产品类别"
              label-width="75px"
              prop="productType"
              size="mini"
            >
              <div class="manager-tags">
                  <span
                    v-for="dict in dict.type.product_type"
                    :key="dict.value"
                    :class="[
                      'manager-tag',
                      queryParams.productType === dict.value ? 'active' : '',
                    ]"
                    @click="handleManagerSelect(dict)"
                  >
                    {{ dict.label }}
                  </span>
              </div>
            </el-form-item>
          </div>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      <div style="width: 100%;height: 90vh;overflow:auto; margin-bottom: 30%;padding-bottom: 18%;position: relative;">
        <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange"
            :row-style="{ height: '30px' }" border :cell-style="{ padding: '0px' }" highlight-current-row>
            <el-table-column label="序." type="index" align="center" prop="index" :index="indexMethod"></el-table-column>
            <el-table-column label="代理情况" align="center" prop="isProxy" :show-overflow-tooltip="true" width="150" />
            <el-table-column label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true" width="300">
            </el-table-column>
          <el-table-column label="产品类别" align="center" prop="productType" :show-overflow-tooltip="true" width="200" >
              <template slot-scope="scope">
                <dict-tag :options="dict.type.product_type" :value="scope.row.productType" />
              </template>
          </el-table-column>
            <el-table-column label="规格" align="center" prop="productSpecs"  :show-overflow-tooltip="true" width="250">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.publisherTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="服务机构" align="center" prop="enterpriseName" width="300" :show-overflow-tooltip="true" />

        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>
    </div>
</template>

<script>
import { listManagement, getManagement, delManagement, addManagement, updateManagement } from "@/api/website/website";
import {productList} from "@/api/list/list";
export default {
    name: "websiteManagement",
    dicts: ['supplier_type','website_type', 'product_type', 'product_status', 'customer_industry', 'administrative_area', 'qualification_resume_status'],
    data() {
        return {
            isShowBtn: true,
            newWebsiteAs: null,
            opt: "edit",
            businessCode: "website",
            businessId: null,
            fileType: 0,
            refreshKey: 0,
            index: null,
            // 日期范围
            dateRange: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 【请填写功能名称】表格数据
            managementList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 15,
                isProxy:'已代理',
                productType: null,
                productNo: null,
                productName: null,
                productStatus: null,
                examineStatus: null,
                classificationId: null,
                organizationName: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                titleName: [
                    { required: true, message: "标题不能为空", trigger: "blur" },
                    { max: 50, message: '标题长度不能超过50', trigger: 'blur' }
                ],
                publisherType: [
                    { required: true, message: "网站类型不能为空", trigger: "change" },
                ],
                websiteName: [
                    { required: true, message: "网站名称不能为空", trigger: "blur" },
                    { max: 400, message: '网站名称长度不能超过400', trigger: 'blur' }
                ],
                publisherTime: [
                    { required: true, message: "发布时间不能为空", trigger: "blur" },
                ],
            }
        };
    },
    created() {
        this.newWebsiteAs = this.$route.query.websiteAs;
        if (this.newWebsiteAs == "1") {
            this.isShowBtn = false;
        } else {
            this.isShowBtn = true;
        }
        this.getList();
    },
    methods: {
      handleManagerSelect(dict) {
        if (this.queryParams.productType === dict.value) {
          this.queryParams.productType = null; // 再次点击取消选择
        } else {
          this.queryParams.productType = dict.value;
        }
        this.handleQuery(); // 触发搜索
      },
        /** 查询【请填写功能名称】列表 */
        // getList() {
        //     this.loading = true;
        //     listManagement(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        //         this.loading = false;
        //     });
        // },
          getList(){
          this.loading = true;
          productList(this.queryParams).then(res=>{
            this.total=res.total;
            this.managementList=res.rows;
            this.loading = false;
          })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                titleName: null,
                websiteName: null,
                publisherId: null,
                orderNum: null,
                publisherName: null,
                publisherTime: null,
                publisherType: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.opt = "add";
            this.fileType = 0;
            this.form.publisherTime = this.showDate();
            this.businessId = this.guid();
            this.open = true;
            this.title = "添加网站管理信息";
            this.refreshKey++;
        },
        /** 修改按钮操作 */
        handleUpdate(row, index) {
            this.reset();
            this.opt = "edit";
            this.fileType = 1;
            this.businessId = row.id;
            const id = row.id;
            this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
            getManagement(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改网站管理信息";
            });
            this.refreshKey++;
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateManagement(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.refreshKey++;
                            this.getList();
                        });
                    } else {
                        this.form.id = this.businessId;
                        addManagement(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.refreshKey++;
                            this.getList();
                        });
                    }
                }
            });
        },
        indexMethod(index) {
            return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        },
        showDate() {
            var mydate = new Date();
            var str = "" + mydate.getFullYear() + "-";
            str += (mydate.getMonth() + 1) <= 9 ? "0" + (mydate.getMonth() + 1) + "-" : mydate.getMonth() + 1 + "-";
            str += mydate.getDate() <= 9 ? "0" + mydate.getDate() : mydate.getDate();
            return str;
        },
        guid() {
            function S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            }
            return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除标题为"' + row.titleName + '"的数据项？').then(function () {
                return delManagement(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        handleClick(url) {
            if (!url.startsWith('http://') && !url.startsWith('https://')) {
                // 如果没有协议，添加默认的http://协议
                url = 'http://' + url;
            } window.open(url, '_blank');
        },
    }
};
</script>
<style scoped>
.manager-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;

  .manager-tag {
    padding: 0 12px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.3s;

    &:hover {
      border-color: #409eff;
      color: #409eff;
    }

    &.active {
      background-color: #409eff;
      border-color: #409eff;
      color: #fff;
    }
  }
}
</style>
